package game.wormgame.mrworm;

import java.util.List;

import game.wormgame.framework.Game;
import game.wormgame.framework.Graphics;
import game.wormgame.framework.Screen;
import game.wormgame.framework.Input.TouchEvent;

public class HighscoreScreen extends Screen {
    String lines[] = new String[5];
    private int font_width = 75;
    private int line_spacing = 65;
    private int spacing_after_period = 75;

    public HighscoreScreen(Game game) {
        super(game);

        for (int i = 0; i < 5; i++) {
            lines[i] = "" + (i + 1) + ". " + Settings.highscores[i];
        }
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();

        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_UP) {
                if (event.x > 906 && event.y > 441) {
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    game.setScreen(new MainMenuScreen(game));
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();

        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.high_scores, 235, 30);

        int y = 125;
        for (int i = 0; i < 5; i++) {
            drawText(g, lines[i], 300, y);
            y += line_spacing;
        }

        g.drawPixmap(Assets.buttons, 906, 441, 0, 200, 100, 100);
    }

    public void drawText(Graphics g, String line, int x, int y) {
        int len = line.length();
        for (int i = 0; i < len; i++) {
            char character = line.charAt(i);
    
            if (character == ' ') {
                x += spacing_after_period;
                continue;
            }
    
            int srcX = 0;
            int srcWidth = 0;
            if (character == '.') {
                srcX = 750;
                srcWidth = 37;
            } else {
                srcX = (character - '0') * font_width;
                srcWidth = font_width;
            }
    
            g.drawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, font_width);
            x += srcWidth;
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}
